<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>商城后台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/layui_ext/dtree/dtree.css}" >
    <link rel="stylesheet" th:href="@{/lib/layui_ext/dtree/font/dtreefont.css}" >
    <link rel="stylesheet" th:href="@{/lib/zTree/css/zTreeStyle/zTreeStyle.css}" >


    <style>
        .layui-table-cell {
            height: auto !important;
            white-space: normal;
        }
        .ztree *{
            font: 14px Helvetica Neue,Helvetica,PingFang SC,Tahoma,Arial,sans-serif;
        }
    </style>

</head>
<body>

<span th:replace="common_page::common"></span>

<div class="layui-layout">
    <div class="layui-side ">

        <div class="layuimini-container" style="height:90%">
            <div class="layuimini-main">
                <div class="layui-card">
                    <div class="layui-card-header">分类树</div>
                    <div class="layui-card-body" >
                        <ul id="categoryTree" class="ztree"></ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-body">

        <div class="layuimini-container">
            <div class="layuimini-main">

                <fieldset class="table-search-fieldset">
                    <div style="margin: 10px 10px 10px 10px">
                        <form class="layui-form layui-form-pane" id="userSearchForm" action="">
                            <input type="hidden" id="categoryId" name="categoryId">
                            <div class="layui-form-item">

                                <div class="layui-inline">
                                    <label class="layui-form-label">店铺商品ID</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="id" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label" style="width:140px;">店铺商品UUID</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="uuid" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">商品名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="name" autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label">上架状态</label>
                                    <div class="layui-input-inline">
                                        <select id="status"  name="status" lay-search="">
                                            <option value="-1" selected>全部</option>
                                            <option value="0" >未上架</option>
                                            <option value="1" >已上架</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">

                                <div class="layui-inline">
                                    <label class="layui-form-label">店铺ID</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="shopId" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button type="submit" id="queryButton" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                                    <button type="reset" class="layui-btn layui-btn-primary" id="data-reset-btn" ><i class="layui-icon"></i> 重 置</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </fieldset>

                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container">
                        <span th:each="tb : ${toolbarButtons}" th:utext="${tb}"></span>

                    </div>
                </script>

                <table class="layui-hide" id="shopProductTable" lay-filter="shopProductTableFilter"></table>

                <script type="text/html" id="shopProductTableBar">
                    <span th:each="btn : ${rowButtons}" th:utext="${btn}"></span>
                </script>
            </div>
        </div>
    </div>
</div>

<input type="hidden" id="pcProductPreviewPage" th:value="${pcProductPreviewPage}"/>

<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.core.js}"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.excheck.js}"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.exedit.js}"></script>
<script>
    layui.config({
        base: basePath+'/lib/layui_ext/dtree/' //配置 layui 第三方扩展组件存放的基础目录
    }).extend({
        dtree: 'dtree' //定义该组件模块名
    }).use(['form', 'table','tree','dtree','jquery'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var index;
        var tree = layui.tree;
        var loadding;
        var loaddingTree;
        var dtree = layui.dtree;

        $.fn.serializeJson=function(){
            var serializeObj={};
            var array=this.serializeArray();
            var str=this.serialize();
            $(array).each(function(){
                if(serializeObj[this.name]){
                    if($.isArray(serializeObj[this.name])){
                        serializeObj[this.name].push(this.value);
                    }else{
                        serializeObj[this.name]=[serializeObj[this.name],this.value];
                    }
                }else{
                    serializeObj[this.name]=this.value;
                }
            });
            return serializeObj;
        };

        function getFormParam()
        {
            var formParam = $("#userSearchForm").serializeJson();
            return formParam;
        }


        table.render({
            elem: '#shopProductTable',
            url: "[[@{/}]]product/shopProduct/list",
            method:'POST',
            where:getFormParam(),
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter',  {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            done:function(res, curr, count)
            {
            },
            cols: [
                [
                    {type: "checkbox", width: 50},
                    {field: 'id',  title: '店铺商品ID', hide:true, width: 150},
                    {field: 'name',width: 400,  title: '商品名称',event:'openDetailDialogDevent',
                        templet: function (row) {
                            return "<a href='#'   style='color: blue;text-decoration:underline;'>"+row.name+"</a>";
                        }},
                    {field: 'httpMainPhotoFilePath',width:150,  title: '商品主图',
                        templet: function (row) {
                            if(row.httpMainPhotoFilePath!=null) {
                                return "<a href='" + row.httpMainPhotoFilePath + "' target='_blank'><img src='" + row.httpMainPhotoFilePath + "' width='100' height='100'  /></a>";
                            }
                            return "";
                        }},
                    // {field: 'uuid',width: 300,  title: '店铺商品UUID'},
                    {field: 'categoryPath',width: 350,  title: '分类路径'},
                    // {field: 'categoryName',width: 150,  title: '分类名称'},
                    // {field: 'brandHttpLogo',width:150,  title: '品牌LOGO',
                    //     templet: function (row) {
                    //         if(row.brandHttpLogo!=null) {
                    //             return "<a href='" + row.brandHttpLogo + "' target='_blank'><img src='" + row.brandHttpLogo + "' width='100' height='100'  /></a>";
                    //         }
                    //         return "";
                    //     }},
                    // {field: 'brandChineseName',width: 150,  title: '品牌中文名称'},
                    // {field: 'brandEnglishName',width: 150,  title: '品牌英文名称'},
                    // {field: 'shopCategoryPath',width: 150,  title: '店铺内分类路径'},
                    // {field: 'shopCategoryName',width: 150,  title: '店铺内分类名称'},
                    // {field: 'productUuid',width: 150,  title: '平台商品的UUID'},
                    // {field: 'productId',width: 150,  title: '平台商品ID'},
                    // {field: 'attributes',width: 550,  title: '属性列表'},
                    {field: 'status',width: 150,   title: '上架状态',
                        templet: function (row) {
                            if(row.status == null){
                                return "";
                            }
                            if(row.status == "0"){
                                return "<a style='color:red'>未上架</a>";
                            }
                            if(row.status == "1"){
                                return "<a style='color:green'>已上架</a>";
                            }
                            return row.status;
                        }
                    },
                    // {field: 'shopId',width: 300,  title: '店铺ID'},
                    // {field: 'shopName',width: 300,  title: '店铺名称'},
                    // {field: 'sellerNo',width: 150,   title: '卖家编码'},
                    // {field: 'articleNumber',width: 150, title: '货号'},
                    // {field: 'payMethod',width: 150,   title: '付款方式',
                    //     templet: function (row) {
                    //         if(row.payMethod == null){
                    //             return "";
                    //         }
                    //         if(row.payMethod == "1"){
                    //             return "一口价(普通交易模式)";
                    //         }
                    //         return row.payMethod;
                    //     }
                    // },
                    // {field: 'buckleInventoryMethod',width: 150,   title: '库存计数',
                    //     templet: function (row) {
                    //         if(row.buckleInventoryMethod == null){
                    //             return "";
                    //         }
                    //         if(row.buckleInventoryMethod == "1"){
                    //             return "买家拍下减库存";
                    //         }
                    //         if(row.buckleInventoryMethod == "2"){
                    //             return "买家付款减库存";
                    //         }
                    //         return row.buckleInventoryMethod;
                    //     }
                    // },
                    // {field: 'giveInvoice',width: 150,   title: '开发票',
                    //     templet: function (row) {
                    //         if(row.giveInvoice == null){
                    //             return "";
                    //         }
                    //         if(row.giveInvoice == "1"){
                    //             return "支持";
                    //         }
                    //         if(row.giveInvoice == "0"){
                    //             return "不支持";
                    //         }
                    //         return row.giveInvoice;
                    //     }
                    // },
                    // {field: 'changeOrReturn',width: 150,   title: '退换货承诺',
                    //     templet: function (row) {
                    //         if(row.changeOrReturn == null){
                    //             return "";
                    //         }
                    //         if(row.changeOrReturn == "1"){
                    //             return "承诺";
                    //         }
                    //         if(row.changeOrReturn == "0"){
                    //             return "不承诺";
                    //         }
                    //         return row.changeOrReturn;
                    //     }
                    // },
                    // {field: 'etractMethod',width: 150,   title: '提取方式',
                    //     templet: function (row) {
                    //         var etractMethodText = "";
                    //         if(row.etractMethod!=null) {
                    //             if (row.etractMethod.indexOf("1") != -1) {
                    //                 etractMethodText += "用物流配送"
                    //             }
                    //         }
                    //         return etractMethodText;
                    //     }
                    // },
                    // {field: 'createUserId',width: 300, title: '店铺掌柜ID'},
                    {field: 'createDate', width: 150, title: '发布时间'},
                    // {field: 'updateDate', width: 150, title: '修改时间'},
                    {title: '操作', minWidth: 300, toolbar: '#shopProductTableBar', align: "center"}
                ]
            ],
            limits: [15,30,100,200],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('shopProductTable', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });


        $("#data-reset-btn").click(function(){
            $("#categoryId").val("");
            $("#userSearchForm")[0].reset();
            layui.form.render();
        });


        var setting = {
            async: {
                enable: true,
                url:"[[@{/}]]product/shopProduct/query/category/tree/pid",
                autoParam:["id", "pid", "name"],
                dataFilter: filter
            },
            data:{
                simpleData:{
                    enable: true,
                    idKey:'id',
                    pIdKey:'parentId',
                    rootPId: -1
                }
            },
            view:{
                showIcon: true
            },
            callback: {
                onClick: categoryTreeClick
            }
        };

        function categoryTreeClick(event, treeId, treeNode) {
            // $("#inp").val("id="+treeNode.id + ", name=" + treeNode.name + ",pId=" + treeNode.pId);
            $("#categoryId").val(treeNode.id);
            $("#queryButton").click();
        }

        initCategoryTree();

        function filter(treeId, parentNode, data) {
            return data.data;
        }
        //初始化树
        function initCategoryTree(){

            loaddingTree = layer.load();
            $.ajax({
                url:"[[@{/}]]product/shopProduct/query/category/tree/pid",
                type:"POST",
                dataType: "json",
                success: function(data){
                    var zTreeObj = $.fn.zTree.init($("#categoryTree"),setting, data.data);
                    layer.close(loaddingTree);
                },
                error: function(){

                },
                complete:function()
                {
                    layer.close(loaddingTree);
                }
            });
        }

        /**
         * toolbar监听事件
         */
        table.on('toolbar(shopProductTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加商品',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,

                    area: ['100%', '100%'],
                    content: "[[@{/product/shopProduct/addPage}]]"
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'deleteToolbar') {  // 监听删除操作
                var checkStatus = table.checkStatus('shopProductTable')
                    , data = checkStatus.data;
                if(data==null||data.length<=0)
                {
                    layer.msg("请选择要操作的记录");
                    return;
                }
                layer.confirm('确定删除商品?', function (index) {
                    $.ajax({
                        url:"[[@{/}]]product/shopProduct/delete/ids",
                        contentType: "application/json; charset=utf-8",
                        type:'DELETE',
                        data:JSON.stringify(data),
                        success:function(data){
                            layer.msg(data.msg);

                            //执行搜索重载
                            table.reload('shopProductTable', {
                                page: {
                                    curr: 1
                                }
                                , where: data.field
                            }, 'data');

                        },
                        complete:function(data)
                        {
                            layer.close(index);
                        }
                    });
                });
            }else if (obj.event === 'flushSearchToolbar') {  // 监听同步搜索操作
                var checkStatus = table.checkStatus('shopProductTable')
                    , data = checkStatus.data;
                if(data==null||data.length<=0)
                {
                    layer.msg("请选择要操作的记录");
                    return;
                }
                var reqData= new Array();
                for(var i=0;i<data.length;i++)
                {
                    reqData.push(data[i].id);
                }
                layer.confirm('确定同步到搜索?', function (index) {
                    $.ajax({
                        url:"[[@{/}]]product/shopProduct/flush/search",
                        contentType: "application/json; charset=utf-8",
                        type:'POST',
                        data:JSON.stringify({ids:reqData}),
                        success:function(data){
                            layer.msg(data.msg);

                            //执行搜索重载
                            table.reload('shopProductTable', {
                                page: {
                                    curr: 1
                                }
                                , where: data.field
                            }, 'data');

                        },
                        complete:function(data)
                        {
                            layer.close(index);
                        }
                    });
                });
            }
        });



        //监听表格复选框选择
        table.on('checkbox(shopProductTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(shopProductTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event == 'detail') {
                openDetailDialog(data.id);
            }else if(obj.event=="openDetailDialogDevent")
            {
                openDetailDialog(data.id);
            }else if (obj.event == 'shelves') {
                shelves(data);
            }else if (obj.event == 'previewPc') {
                previewPc(data);
            }
        });

        function previewPc(data)
        {
            var pcProductPreviewPage = $("#pcProductPreviewPage").val();
            window.open(pcProductPreviewPage+data.id)
        }

        function shelves(row)
        {
            var statusValue="";
            if(row.status == "0"){
                statusValue="上架";
            }
            if(row.status == "1"){
                statusValue="下架";
            }
            layer.confirm("确定要【"+statusValue+"】\""+row.name+"\"?", function (index) {
                $.ajax({
                    url:"[[@{/}]]product/shopProduct/shelves",
                    contentType: "application/json; charset=utf-8",
                    type:'POST',
                    data:JSON.stringify({id:row.id,shopId:row.shopId}),
                    success:function(data){
                        layer.msg(data.msg);

                        //执行搜索重载
                        table.reload('shopProductTable', {
                            page: {
                                curr: 1
                            }
                            , where: data.field
                        }, 'data');

                    },
                    complete:function(data)
                    {
                        layer.close(index);
                    }
                });
            });
        }

        function openDetailDialog(id)
        {
            var index = layer.open({
                title: '商品详情',
                type: 2,
                shade: 0.2,
                maxmin:true,

                area: ['100%', '100%'],
                content: '[[@{/}]]product/shopProduct/detailPage/'+id
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;

        }

    });
</script>


</body>
</html>